package com.RedRiver.hospitalsystem.finance_service.service.impl;

import com.RedRiver.hospitalsystem.finance_service.common.Finance_serviceResultCode;
import com.RedRiver.hospitalsystem.finance_service.common.WoniuxyBeanCopyUtil;
import com.RedRiver.hospitalsystem.finance_service.common.exception.BillException;
import com.RedRiver.hospitalsystem.finance_service.common.exception.InvoiceException;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.BillMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.mapper.InvoiceMapper;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Bill;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Invoice;
import com.RedRiver.hospitalsystem.finance_service.service.InvoiceService;
import com.RedRiver.hospitalsystem.finance_service.service.from.CreateInvoiceFrom;
import com.RedRiver.hospitalsystem.finance_service.service.from.FindInvoicePageFromList;
import com.RedRiver.hospitalsystem.finance_service.service.info.*;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class InvoiceServiceImpl extends ServiceImpl<InvoiceMapper, Invoice> implements InvoiceService {

    @Resource
    private InvoiceMapper invoiceMapper;
    @Resource
    private BillMapper billMapper;
    @Override
    public CreateInvoiceInfo createInvoice(CreateInvoiceFrom copyone) {
        Invoice copyone1 = WoniuxyBeanCopyUtil.copyone(copyone, Invoice.class);
        QueryWrapper<Invoice> wrapper = new QueryWrapper<Invoice>().eq("bill_id", copyone1.getBillId());
        Invoice invoice = invoiceMapper.selectOne(wrapper);
        if (invoice!=null)throw new InvoiceException(50001,"该订单已开发票");
        Bill bill = billMapper.selectById(copyone1.getBillId());
        if (null==bill)throw new BillException(50001,"该账单不存在");
        String origin="1234567890";
        StringBuilder s = new StringBuilder();
        for (int i=0;i<9;i++){
            s.append(origin.charAt(new Random().nextInt(origin.length())));
        }
        copyone1.setInvoiceId(Integer.parseInt(s.toString()));
        copyone1.setInvoiceTotal(bill.getBillPrice());
        copyone1.setInvoiceState(0);
        int insert = invoiceMapper.insert(copyone1);
        if (insert>0)return WoniuxyBeanCopyUtil.copyone(copyone1,CreateInvoiceInfo.class);
        else throw new InvoiceException(50001,"发票创建失败");
    }

    @Override
    public FindInvoiceListInfo findInvoiceAll(FindInvoicePageFromList copyone) {
        QueryWrapper<Invoice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (copyone.getStart()!=null&&copyone.getStop()!=null){
            wrapper.between("create_time",copyone.getStart(),copyone.getStop());
        }
        Page<Invoice> page = new Page<>(copyone.getCurrent(), copyone.getSize());
        IPage<Invoice> page1 = page(page,wrapper);
        List<Invoice> records = page1.getRecords();
        if (null==records||records.size()==0)throw new BillException(50001,"未查询到");
        FindInvoiceListInfo findInvoiceListInfo = new FindInvoiceListInfo();
        List<FindInvoiceInfo> infos = WoniuxyBeanCopyUtil.copyList(records, FindInvoiceInfo.class);
        findInvoiceListInfo.setList(infos);
        findInvoiceListInfo.setCurrent(page1.getCurrent());
        findInvoiceListInfo.setSize(page1.getSize());
        findInvoiceListInfo.setTotal(page1.getTotal());
        return findInvoiceListInfo;
    }

    @Override
    public CreateInvoiceInfo changeInvoice(CreateInvoiceFrom copyone) {
        Invoice copyone1 = WoniuxyBeanCopyUtil.copyone(copyone, Invoice.class);
        QueryWrapper<Invoice> wrapper = new QueryWrapper<Invoice>().eq("bill_id", copyone1.getBillId());
        Invoice invoice = invoiceMapper.selectOne(wrapper);
        if (invoice==null)throw new InvoiceException(50001,"该订单还未开发票");
        if(invoice.getInvoiceState()==1)throw new InvoiceException(50001,"发票已结算，不可换开");
        Bill bill = billMapper.selectById(copyone1.getBillId());
        if (null==bill)throw new BillException(50001,"该账单不存在");
        invoiceMapper.deleteById(invoice);
        String origin="1234567890";
        StringBuilder s = new StringBuilder();
        for (int i=0;i<9;i++){
            s.append(origin.charAt(new Random().nextInt(origin.length())));
        }
        copyone1.setInvoiceId(Integer.parseInt(s.toString()));
        copyone1.setInvoiceTotal(bill.getBillPrice());
        copyone1.setInvoiceState(0);
        int insert = invoiceMapper.insert(copyone1);
        if (insert>0)return WoniuxyBeanCopyUtil.copyone(copyone1,CreateInvoiceInfo.class);
        else throw new InvoiceException(50001,"发票创建失败");
    }
}
